//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 5
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace DeleteItems
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Create some items to delete
            //
            ItemIdType message1 = EWSHelpers.CreateMessage(binding, "Message #1");
            ItemIdType message2 = EWSHelpers.CreateMessage(binding, "Message #2");
            ItemIdType message3 = EWSHelpers.CreateMessage(binding, "Message #3");

            DeleteItems(
                    binding, 
                    DisposalType.HardDelete,
                    new ItemIdType[] { message1, message2, message3 });
        }

        /// <summary>
        /// Delete items passed in and write results to console (Listing 5-18)
        /// </summary>
        /// <param name="binding">binding to use for web method call</param>
        /// <param name="deleteType">Delete type to use</param>
        /// <param name="itemsToDelete">params array of items to delete</param>
        /// 
        public static void DeleteItems(
                               ExchangeServiceBinding binding,
                               DisposalType deleteType,
                               params ItemIdType[] itemsToDelete)
        {
            DeleteItemType request = new DeleteItemType();
            request.DeleteType = deleteType;
            request.ItemIds = itemsToDelete;
            DeleteItemResponseType response = binding.DeleteItem(request);

            // Since there really isn't anything interesting in our response 
            // messages, we can just use the base class for the response messages.  
            // Of course, if we really want to deal with the actual class, 
            // DeleteItem returns ItemInfoResponseMessageType -- of course, the 
            // Items collection will always be empty.
            //
            int index = 0;
            foreach (ResponseMessageType responseMessage in
                                    response.ResponseMessages.Items)
            {
                Console.WriteLine("Item #{0} - result: {1}", index++,
                            responseMessage.ResponseCode.ToString());
            }
        }

    }
}
